/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.policy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.policy.entity.PolicyDrawMaterial;
import org.springblade.modules.policy.service.IPolicyDrawMaterialService;
import org.springblade.modules.policy.vo.PolicyDrawMaterialVO;
import org.springblade.modules.system.entity.File;
import org.springblade.modules.system.service.IFileService;
import org.springblade.modules.system.vo.AvueUploadVo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 申请材料表 控制器
 *
 * @author Blade
 * @since 2022-07-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/blade-policy/policydrawmaterial")
@Api(value = "申请材料表", tags = "申请材料表接口")
public class PolicyDrawMaterialController extends BladeController {

	private IPolicyDrawMaterialService policyDrawMaterialService;

	private IFileService fileService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入policyDrawMaterial")
	public R<PolicyDrawMaterialVO> detail(PolicyDrawMaterial policyDrawMaterial) {
		PolicyDrawMaterial detail = policyDrawMaterialService.getOne(Condition.getQueryWrapper(policyDrawMaterial));
		PolicyDrawMaterialVO policyDrawMaterialVO = new PolicyDrawMaterialVO();
		BeanUtils.copyProperties(detail, policyDrawMaterialVO);
		if (Func.isNotBlank(detail.getFiles())) {
			LambdaQueryWrapper<File> fileLambdaQueryWrapper = Wrappers.lambdaQuery();
			fileLambdaQueryWrapper.in(File::getId, (Object[]) detail.getFiles().split(","));
			List<AvueUploadVo> list = new ArrayList<>(16);
			List<File> list1 = fileService.list(fileLambdaQueryWrapper);
			list1.forEach(i -> {
				AvueUploadVo avueUploadVo = new AvueUploadVo();
				avueUploadVo.setValue(i.getId() + "");
				avueUploadVo.setLabel(i.getOriginal());
				list.add(avueUploadVo);
			});
			policyDrawMaterialVO.setFileList(list);
		}
		return R.data(policyDrawMaterialVO);
	}

	/**
	 * 分页 申请材料表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入policyDrawMaterial")
	public R<IPage<PolicyDrawMaterial>> list(PolicyDrawMaterial policyDrawMaterial, Query query) {
		IPage<PolicyDrawMaterial> pages = policyDrawMaterialService.page(Condition.getPage(query), Condition.getQueryWrapper(policyDrawMaterial));
		return R.data(pages);
	}

	/**
	 * 自定义分页 申请材料表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入policyDrawMaterial")
	public R<IPage<PolicyDrawMaterialVO>> page(PolicyDrawMaterialVO policyDrawMaterial, Query query) {
		IPage<PolicyDrawMaterialVO> pages = policyDrawMaterialService.selectPolicyDrawMaterialPage(Condition.getPage(query), policyDrawMaterial);
		return R.data(pages);
	}

	/**
	 * 新增 申请材料表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入policyDrawMaterial")
	public R save(@Valid @RequestBody PolicyDrawMaterial policyDrawMaterial) {
		return R.status(policyDrawMaterialService.save(policyDrawMaterial));
	}

	/**
	 * 修改 申请材料表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入policyDrawMaterial")
	public R update(@Valid @RequestBody PolicyDrawMaterial policyDrawMaterial) {
		if (policyDrawMaterialService.updateById(policyDrawMaterial)) {
			return R.data(policyDrawMaterial);
		} else {
			return R.fail("操作失败");
		}
	}

	/**
	 * 新增或修改 申请材料表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入policyDrawMaterial")
	public R submit(@Valid @RequestBody PolicyDrawMaterial policyDrawMaterial) {
		if (policyDrawMaterialService.saveOrUpdate(policyDrawMaterial)) {
			return R.data(policyDrawMaterial);
		} else {
			return R.fail("操作失败");
		}
	}


	/**
	 * 删除 申请材料表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(policyDrawMaterialService.deleteLogic(Func.toLongList(ids)));
	}


}
